<?php

namespace app\admin\controller;

//use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Session;
use think\Model;


/*
 * 营销管理
 * WANGTIANTIAN
 */

class Market extends Base
{

    /*
     * 皮肤列表
     * wangtiantian
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize = input('limit') ? input('limit') : $conf['paginate']['list_rows'];

        // 查询数据

        // 查询数据
        $model = Db::name('skin');
        $list = $model->where('is_del', 0)->where('type', 0)->order("id desc")->paginate($pageSize);
        $aa = $list->toArray();
        $res = $aa['data'];
        foreach ($res as $k => $v) {
            $res[$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
        }
        $this->assign('total', $list->total());
        $this->assign('page', $list->render());
        $this->assign('list', $res);
        return $this->fetch();
    }

    /**
     * 添加皮肤
     * wangtiantian
     */
    public function add()
    {
        if (request()->isPost()) {

            $data = input();
            $model = Db::name('skin');


            $title = $data['title'];
            if ($title == '' || $title == "undefined") {
                $this->error("为填写皮肤主题");
            }

            //拿到图片资源
            $file = $data['ImgPreview']; //赚字图标
            $file2 = $data['ImgPreview2'];//滑动条
            $file9 = $data['ImgPreview9'];//滑动按钮

            $file3 = $data['ImgPreview3'];//背景图片
            $file4 = $data['ImgPreview4'];//电话图标
            $file41 = $data['ImgPreview41'];//金牌顾问
            $file42 = $data['ImgPreview42'];//一键拨打
            $file43 = $data['ImgPreview43'];//小电话图标

            $file5 = $data['ImgPreview5'];//立即报名
            $file51 = $data['ImgPreview51'];//分享赚红包
            $file52 = $data['ImgPreview52']; //姓名
            $file53 = $data['ImgPreview53'];//手机号
            $file54 = $data['ImgPreview54'];//支付按钮


            $file6 = $data['ImgPreview6'];//排行图标
            $file7 = $data['ImgPreview7'];//底部背景
            $file8 = $data['ImgPreview8'];//排行榜背景图


            //验证上传图片---赚字图片
            if ($file == '' || $file == "undefined") {
                $this->error("赚字图片未上传");

            }
            //验证上传图片---滚动条图片
            if ($file2 == '' || $file2 == "undefined") {
                $this->error('滚动条图片未上传');
            }
            //验证上传图片---滑动按钮
            if ($file9 == '' || $file9 == "undefined") {
                $this->error('滑动按钮图片未上传');
            }
            //验证上传图片---背景图片
            if ($file3 == '' || $file3 == "undefined") {
                $this->error('背景图片未上传');
            }
            //验证上传图片---电话图标
            if ($file4 == '' || $file4 == "undefined") {
                $this->error('电话图标未上传');
            }
            //验证上传图片---金牌顾问
            if ($file41 == '' || $file41 == "undefined") {
                $this->error('金牌顾问图片未上传');
            }
            //验证上传图片---一键拨打
            if ($file42 == '' || $file42 == "undefined") {
                $this->error('一键拨打图标未上传');
            }
            //验证上传图片---小电话图标
            if ($file43 == '' || $file43 == "undefined") {
                $this->error('小电话图标未上传');
            }
            //验证上传图片---立即报名图标
            if ($file5 == '' || $file5 == "undefined") {
                $this->error('立即报名图标未上传');
            }
            //验证上传图片---分享赚红包
            if ($file51 == '' || $file51 == "undefined") {
                $this->error('分享赚红包图片未上传');
            }
            //验证上传图片---姓名图标
            if ($file52 == '' || $file52 == "undefined") {
                $this->error('姓名图片未上传');
            }
            //验证上传图片---手机号图标
            if ($file53 == '' || $file53 == "undefined") {
                $this->error('手机号图片未上传');
            }
            //验证上传图片---支付按钮图标
            if ($file54 == '' || $file54 == "undefined") {
                $this->error('支付按钮图片未上传');
            }
            //验证上传图片---排行图标
            if ($file6 == '' || $file6 == "undefined") {
                $this->error('排行图片未上传');
            }
            //验证上传图片---底部背景
            if ($file7 == '' || $file7 == "undefined") {
                $this->error('底部背景图片未上传');
            }
            //验证上传图片---排行榜背景图
            if ($file8 == '' || $file8 == "undefined") {
                $this->error('排行榜背景图未上传');
            }

            // 入库数据


            // 存入数据库
            $data1['title'] = $data['title'];
            $data1['pic_url'] = $data['ImgPreview'];;   //赚字图标
            $data1['pic_url2'] = $data['ImgPreview2']; //滑动条
            $data1['pic_url9'] = $data['ImgPreview9']; //滑动按钮
            $data1['pic_url3'] = $data['ImgPreview3']; //背景图片
            $data1['pic_url4'] = $data['ImgPreview4']; //电话图标
            $data1['pic_url41'] = $data['ImgPreview41']; //金牌顾问
            $data1['pic_url42'] = $data['ImgPreview42']; //一键拨打
            $data1['pic_url43'] = $data['ImgPreview43']; //小电话图标
            $data1['pic_url5'] = $data['ImgPreview5']; //立即报名
            $data1['pic_url51'] = $data['ImgPreview51']; //分享赚红包
            $data1['pic_url52'] = $data['ImgPreview52']; //姓名
            $data1['pic_url53'] = $data['ImgPreview53']; //手机号
            $data1['pic_url54'] = $data['ImgPreview54']; //支付按钮
            $data1['pic_url6'] = $data['ImgPreview6']; //排行图标
            $data1['pic_url7'] = $data['ImgPreview7']; //底部背景
            $data1['pic_url8'] = $data['ImgPreview8']; //排行榜背景图
            $data1['create_time'] = time();
            $data1['is_del'] = 0;
            $data1['is_show'] = 1;


            $ret = $model->insert($data1);

            if ($ret) {
                $this->success("成功");
            } else {
                $this->error("网络异常");
            }

        }

        $re = Db::name('music')
            ->where('is_del', 0)
            ->select();
        $this->assign('re', $re);

        return $this->fetch();
    }

    /*
     * wangtiantian
     * 删除皮肤
     */
    public function del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('skin')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

    /**
     * @throws
     * 编辑皮肤
     * wangtiantian
     */
    public function edit()
    {
        $id = input('id');

        $model = Db::name('skin');

        // 更新数据
        if (request()->isPost()) {

            $data = input();
            $model = Db::name('skin');


            $title = $data['title'];
            if ($title == '' || $title == "undefined") {
                $this->error("为填写皮肤主题");
            }


            //拿到图片资源
            $file = $data['ImgPreview']; //赚字图标
            $file2 = $data['ImgPreview2'];//滑动条
            $file9 = $data['ImgPreview9'];//滑动按钮

            $file3 = $data['ImgPreview3'];//背景图片
            $file4 = $data['ImgPreview4'];//电话图标
            $file41 = $data['ImgPreview41'];//金牌顾问
            $file42 = $data['ImgPreview42'];//一键拨打
            $file43 = $data['ImgPreview43'];//小电话图标

            $file5 = $data['ImgPreview5'];//立即报名
            $file51 = $data['ImgPreview51'];//分享赚红包
            $file52 = $data['ImgPreview52']; //姓名
            $file53 = $data['ImgPreview53'];//手机号
            $file54 = $data['ImgPreview54'];//支付按钮


            $file6 = $data['ImgPreview6'];//排行图标
            $file7 = $data['ImgPreview7'];//底部背景
            $file8 = $data['ImgPreview8'];//排行榜背景图


            //验证上传图片---赚字图片
            if ($file == '' || $file == "undefined") {
                $this->error("赚字图片未上传");

            }
            //验证上传图片---滚动条图片
            if ($file2 == '' || $file2 == "undefined") {
                $this->error('滚动条图片未上传');
            }
            //验证上传图片---滑动按钮
            if ($file9 == '' || $file9 == "undefined") {
                $this->error('滑动按钮图片未上传');
            }
            //验证上传图片---背景图片
            if ($file3 == '' || $file3 == "undefined") {
                $this->error('背景图片未上传');
            }
            //验证上传图片---电话图标
            if ($file4 == '' || $file4 == "undefined") {
                $this->error('电话图标未上传');
            }
            //验证上传图片---金牌顾问
            if ($file41 == '' || $file41 == "undefined") {
                $this->error('金牌顾问图片未上传');
            }
            //验证上传图片---一键拨打
            if ($file42 == '' || $file42 == "undefined") {
                $this->error('一键拨打图标未上传');
            }
            //验证上传图片---小电话图标
            if ($file43 == '' || $file43 == "undefined") {
                $this->error('小电话图标未上传');
            }
            //验证上传图片---立即报名图标
            if ($file5 == '' || $file5 == "undefined") {
                $this->error('立即报名图标未上传');
            }
            //验证上传图片---分享赚红包
            if ($file51 == '' || $file51 == "undefined") {
                $this->error('分享赚红包图片未上传');
            }
            //验证上传图片---姓名图标
            if ($file52 == '' || $file52 == "undefined") {
                $this->error('姓名图片未上传');
            }
            //验证上传图片---手机号图标
            if ($file53 == '' || $file53 == "undefined") {
                $this->error('手机号图片未上传');
            }
            //验证上传图片---支付按钮图标
            if ($file54 == '' || $file54 == "undefined") {
                $this->error('支付按钮图片未上传');
            }
            //验证上传图片---排行图标
            if ($file6 == '' || $file6 == "undefined") {
                $this->error('排行图片未上传');
            }
            //验证上传图片---底部背景
            if ($file7 == '' || $file7 == "undefined") {
                $this->error('底部背景图片未上传');
            }
            //验证上传图片---排行榜背景图
            if ($file8 == '' || $file8 == "undefined") {
                $this->error('排行榜背景图未上传');
            }

            // 入库数据


            // 存入数据库
            $data1['title'] = $data['title'];
            $data1['pic_url'] = $data['ImgPreview'];;   //赚字图标
            $data1['pic_url2'] = $data['ImgPreview2']; //滑动条
            $data1['pic_url9'] = $data['ImgPreview9']; //滑动按钮
            $data1['pic_url3'] = $data['ImgPreview3']; //背景图片
            $data1['pic_url4'] = $data['ImgPreview4']; //电话图标
            $data1['pic_url41'] = $data['ImgPreview41']; //金牌顾问
            $data1['pic_url42'] = $data['ImgPreview42']; //一键拨打
            $data1['pic_url43'] = $data['ImgPreview43']; //小电话图标
            $data1['pic_url5'] = $data['ImgPreview5']; //立即报名
            $data1['pic_url51'] = $data['ImgPreview51']; //分享赚红包
            $data1['pic_url52'] = $data['ImgPreview52']; //姓名
            $data1['pic_url53'] = $data['ImgPreview53']; //手机号
            $data1['pic_url54'] = $data['ImgPreview54']; //支付按钮
            $data1['pic_url6'] = $data['ImgPreview6']; //排行图标
            $data1['pic_url7'] = $data['ImgPreview7']; //底部背景
            $data1['pic_url8'] = $data['ImgPreview8']; //排行榜背景图
            $data1['update_time'] = time();
            $data1['is_del'] = 0;
            $data1['is_show'] = 1;


            $ret = $model->where('id', $id)->update($data1);
            if ($ret) {
                $this->success("更新成功");
            } else {
                $this->error("更新失败");
            }

        }


        // 详情
        $info = $model->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    /*
     * 开启和关闭皮肤操作
     * wangtiantian
     */
    public function openclose()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $res = Db::name('skin')->where('id', $id)->update(['is_show' => input('is_show')]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /*
     * 添加音乐
     * wangtiantian
     */
    public function addmusic()
    {


        if (request()->isAjax()) {

            $data = input();

            $table = Db::name('music');

            if ($data['title'] == '') {
                $this->error("请填写音乐名称");
            }
            if ($data['music'] == '') {
                $this->error("请添加音乐");
            }

            $d['title'] = $data['title'];
            $d['m_url'] = $data['music'];
            $d['create_time'] = time();

            $res = $table->insertGetId($d);
            if ($res) {
                $this->success("添加成功");
            } else {
                $this->error("添加失败");
            }
        }
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize = input('limit') ? input('limit') : $conf['paginate']['list_rows'];

        // 查询数据

        // 查询数据
        $model = Db::name('music');
        $list = $model
            ->where('is_del', 0)
//           ->where('type',0)
            ->order("id desc")->paginate($pageSize);
        $aa = $list->toArray();
        $res = $aa['data'];
        foreach ($res as $k => $v) {
            $res[$k]['create_time'] = date('Y-m-d H:s', $v['create_time']);
        }
        $this->assign('total', $list->total());
        $this->assign('page', $list->render());
        $this->assign('list', $res);
        return $this->fetch();
    }

    /*
     * wangtiantian
     * 删除音乐
     */
    public function m_del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('music')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }


    /*
     * 预览页面
     * wangtian
     */
    // 获取皮肤id
    public function qr()
    {

        if (request()->isAjax()) {
            //活动id
            session::set("skin_id", input('id'));
            if (session::get("skin_id")) {
                $this->success();
            }
            $this->error();
        }

        $this->assign('skin_id', session::get("skin_id"));
        return $this->fetch('index');
    }
    // 皮肤预览
    public function preview()
    {
        // 活动id
        $skin_id = session::get("skin_id");
        $skin = Db::name('skin')->where('id', $skin_id)->find();
        $this->assign('skin', $skin);

        return $this->fetch();
    }
}


?>